/**
 * 帧
 * User: Zhikang.Peng
 * Date: 2018/03/24 15:16
 */

package rtda

import "gitee.com/thghh/jvmgo/rtda/heap"

/**
 * lower 链表结构的下一个节点
 * localVar 局部变量表
 * operandStack 操作数栈指针
 * thread 线程对象
 * pc 下一个命令地址
 */
type Frame struct {
	lower        *Frame
	localVars    LocalVars
	operandStack *OperandStack
	thread       *Thread
	method       *heap.Method
	pc           int
}

func newFrame(thread *Thread, method *heap.Method) *Frame {
	return &Frame{
		thread:       thread,
		method:       method,
		localVars:    newLocalVars(method.MaxLocals()),
		operandStack: newOperandStack(method.MaxStack()),
	}
}

func NewShimFrame(thread *Thread, ops *OperandStack) *Frame {
	return &Frame{
		thread:       thread,
		method:       heap.ShimReturnMethod(),
		operandStack: ops,
	}
}

func (self *Frame) LocalVars() LocalVars {
	return self.localVars
}

func (self *Frame) OperandStack() *OperandStack {
	return self.operandStack
}

func (self *Frame) Thread() *Thread {
	return self.thread
}

func (self *Frame) Method() *heap.Method {
	return self.method
}

func (self *Frame) NextPC() int {
	return self.pc
}

func (self *Frame) SetNextPC(nextPC int) {
	self.pc = nextPC
}

/**
类初始化的时候，需要终止指令的执行,
但是由于此时指令已经执行到了一半，也
就是说当前帧的nextPC字段已经指向下一条指令，
所以需要修改nextPC，让它重新指向当前指令
*/
func (self *Frame) RevertNextPC() {
	self.pc = self.thread.pc
}
